/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.additional;

import com.sun.lwuit.Image;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

/**
 *
 * @author Kvest
 */
public class MenuList extends List implements ActionListener
{
    private static final int DIGIT_SIZE = 12;
    private static Image digits;
    
    private MenuListSelection listener;
    
    public static void setDigits(Image value)
    {
        digits = value;
    }
    
    public MenuList()
    {
        super();
        
        setSelectCommandText("");
        setRenderer(new MenuListRenderer());
        setFixedSelection(List.FIXED_NONE_CYCLIC);

        addActionListener(this);
    }
    
    public void addItem(Object item)
    {
        if ((item instanceof CellItem) && size() < 9)
        {
            ((CellItem)item).setIcon(getDigit(size() + 1));
        }
        
        super.addItem(item);
    }

    public void setSelectionListener(MenuListSelection listener) 
    {
        this.listener = listener;
    }

    public int getSelectedId()
    {
        CellItem selected = (CellItem)getSelectedItem();
        if (selected != null && selected.isEnabled())
        {
            return selected.getId();
        }
        else
        {
            return -1;
        }   
    }
    
    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.itemSelected(getSelectedId());
        }
        
        evt.consume();
    }
    
    private Image getDigit(int digit)
    {
        if (digits != null)
        {
            return digits.subImage(DIGIT_SIZE * (digit - 1), 0, DIGIT_SIZE, DIGIT_SIZE, true);
        }
        
        return null;
    }
    
    public interface MenuListSelection
    {
        public void itemSelected(int id);
    }
}
